package com.carrent.demo.Service.Impl;

import com.carrent.demo.Domain.*;
import com.carrent.demo.Mapper.CarsMapper;
import com.carrent.demo.Mapper.ContractMapper;
import com.carrent.demo.Mapper.UsersMapper;
import com.carrent.demo.Service.CarsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class CarsServiceImpl implements CarsService {

    @Autowired
    CarsMapper carsMapper;

    @Autowired
    UsersMapper usersMapper;

    @Autowired
    ContractMapper contractMapper;

    @Override
    public List<Cars> getPage(Integer start, Integer pageSize, String name) {
        final List<Cars> page = carsMapper.getPage(start, pageSize, name);
        return page;
    }

    @Override
    public Integer getCount(String name) {
        return carsMapper.getCount(name);
    }

    @Override
    public List<Cars> getPage2(Integer start, Integer pageSize) {
        final List<Cars> page2 = carsMapper.getPage2(start, pageSize);
        return page2;
    }

    @Override
    public Integer getCount2() {
        return carsMapper.getCount2() ;
    }

    @Override
    public Boolean deleteCar(String id) {
        carsMapper.deleteCarComplited(Integer.parseInt(id));
        carsMapper.deleteCarContract(Integer.parseInt(id));
        System.out.println(id);
        final Boolean aBoolean = carsMapper.deleteCar(id);
        return  aBoolean;
    }

    @Override
    public Boolean editStatus(Integer id, Integer status) {
        final Boolean aBoolean = carsMapper.editStatus(id, status);
        return  aBoolean;
    }
    @Override
    public Boolean editCar(Cars cars) {
        final Boolean aBoolean = carsMapper.editCar(cars);
        return aBoolean;
    }

    @Override
    public Boolean addCar(Cars cars) {
        final Boolean aBoolean = carsMapper.addCar(cars);
        return  aBoolean;
    }

    @Override
    public List<EmployeeInformation> selectEmployee() {
        return  carsMapper.selectEmployee();
    }

    @Override
    public Boolean addContract(ContractDto contractDto, String userId) throws ParseException {
        Contract contract = new Contract();
        contract.setUserName(usersMapper.searchNameById(Integer.parseInt(userId)));
        contract.setLicensePlate(contractDto.getLp());
        contract.setUserId(Integer.parseInt(userId));
        contract.setEmpId(contractDto.getEmpId());
        contract.setCarId(contractDto.getCarId());
        String beginDate = contractDto.getStartDate();
        String endDate = contractDto.getEndDate();
        Long day = getDaySub(beginDate,endDate);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        final Date begin = format.parse(beginDate);
        contract.setStarDate(begin);
        contract.setStarTime(contractDto.getStartTime() );
        contract.setLeaDay(day.intValue());
        final LocalDateTime now = LocalDateTime.now();
        final Long daySub = getDaySub(now.toString().substring(0,10), beginDate);
        if(daySub>=0)
        contract.setBookPrice(daySub*carsMapper.getCarPrice(contractDto.getCarId())/2);
        else contract.setBookPrice(0.0);
        contract.setLeaPrice(day*carsMapper.getCarPrice(contractDto.getCarId())+contract.getBookPrice());
        contract.setFacMoney(contract.getLeaPrice());
        contractMapper.addContract(contract);
        carsMapper.editStatus(contractDto.getCarId(),0);
        return true;
    }

    public static Long getDaySub(String beginDateStr, String endDateStr) {

        Long day = 0L;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate;
        Date endDate;
        try {
            beginDate = format.parse(beginDateStr);
            endDate = format.parse(endDateStr);
            day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
        } catch (ParseException ignored) {

        }
     //   System.out.println("day:" + day);

        return day;
    }
}



